package com.snail.readyvr.fragment;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.adapter.CommonAdapter;
import com.snail.readyvr.adapter.ViewHolder;
import com.snail.readyvr.mvp.contract.AddVideoActivityContract;
import com.snail.readyvr.mvp.model.VideoHistory;
import com.snail.readyvr.util.DialogUtils;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.StringUtils;
import com.snail.readyvr.util.ToastUtils;
import com.snail.readyvr.util.Utils;

import java.util.List;

import butterknife.BindView;

/**
 * Created by MRKING on 2016/11/17.
 */

public class VideoHistoryFragment extends BaseFragment implements AddVideoActivityContract.View {
    @BindView(R.id.video_history_list)
    ListView mVideoHistoryList;
    @BindView(R.id.no_history_tip)
    TextView mTip;
    private AddVideoActivityContract.Presenter mPresenter;

    private CommonAdapter<VideoHistory> mAdapter;

    private boolean hasCallData = false;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video_history;
    }

    @Override
    protected void init() {
        mAdapter = new CommonAdapter<VideoHistory>(getContext(), R.layout.video_history_item) {
            @Override
            public void convert(ViewHolder helper, final VideoHistory item, int position) {
                if (helper == null || item == null)
                    return;
                helper.setText(R.id.add_new_video_time, item.getAddTime())
                        .setText(R.id.add_new_video_address, item.getVideoUrl());
                helper.setOnClickListener(R.id.video_history_list_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        StartActivityUtils.startVideoPlayer(getContext(),item.getVideoUrl());
                        getActivity().finish();
                    }
                });
            }
        };

        mVideoHistoryList.setAdapter(mAdapter);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden && !hasCallData){
            hasCallData = true;
            String userName = "visitor";
            if (Utils.sUser != null && !StringUtils.isBlank(Utils.sUser.getUserName()))
                userName = Utils.sUser.getUserName();
            mPresenter.findAllVideoHistory(userName);
        }
    }


    @Override
    public void setPresenter(AddVideoActivityContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void findAllVideoHistorySuccess(List<VideoHistory> videoHistories) {

        if (videoHistories != null && videoHistories.size() > 0) {
            mAdapter.addData(videoHistories, true, true);
            mTip.setVisibility(View.GONE);
        }else {
            mTip.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void deleteAllVideosSuccess() {
        mAdapter.clearAllData();
        mTip.setVisibility(View.VISIBLE);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void error(Throwable throwable) {
        ToastUtils.show(getContext(), throwable.getMessage() + "");
    }

    public void deleteAll(){
        if(mTip.getVisibility() == View.VISIBLE){
            ToastUtils.show(getContext(),R.string.no_video_history_tip);
        }else {
            DialogUtils.showEmialTip(getActivity(), getString(R.string.delete_all_video_history_title)
                    , getString(R.string.delete_all_video_history_tip)
                    , null, getString(R.string.delete_ok_tip), true
                    , new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mPresenter.deleteAllVideos();
                        }
                    });
        }
    }
}
